<?php

class ModelAction extends BaseAction {


    public function _initialize(){
        parent::_initialize();
    
    }

    public function index(){
         import('ORG.Util.Page');// 导入分页类
        $count  = D('model')->count();  // 查询满足要求的总记录数
        $Page   = new Page($count,25);       // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show   = $Page->show();                    // 分页显示输出
        $list = D('model')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);                    // 赋值数据集
        $this->assign('page',$show);                    // 赋值分页输出
        $this->show();
    }


    public function addmodel(){
        $acname  = '新增模型栏目';  
        $posturl =  U('Model/doaddmodel');
        $this->assign('acname',$acname);
        $this->assign('posturl',$posturl);
        $this->show();
    }

    public function updatemodel(){
        $map['mid'] = I('get.mid');
        $data = D('model')->where($map)->find();
        $acname  = '修改模型栏目';  
        $posturl =  U('Model/doupdatemodel');
        $this->assign('acname',$acname);
        $this->assign('posturl',$posturl);
        $this->assign('data',$data);
        $this->show('addmodel');
    }

    public function doaddmodel(){
        $info = I('post.');
        $data = array('title'=>$info['title'],'table'=>$info['table'],'act_add'=>$info['act_add'],'act_update'=>$info['act_update']);
        $add  = D('model')->add($data);
        if($add){
            $this->success('新增模型成功！',U('Model/index'));
        }else{
            $this->error('新增模型失败！',U('Model/addmodel'));
        }
    }

    public function doupdatemodel(){
        $info = I('post.');
        $map['mid'] = $info['mid'];
        $data = array('title'=>$info['title'],'table'=>$info['table'],'act_add'=>$info['act_add'],'act_update'=>$info['act_update']);
        $save = D('model')->where($map)->save($data);
        if($save){
            $this->success('修改模型成功！',U('Model/index'));
        }else{
            $this->error('修改模型失败！',U('Model/updatemodel',array('mid'=>$info['mid'])));
        }
    }

    public function delmodel(){
        $map['mid'] = I('get.mid');
        $del = D('model')->where($map)->delete();
        if($del){
            $this->success('删除模型成功！',U('Model/index'));
        }else{
            $this->error('删除模型失败！',U('Model/index'));
        }
    }


    public function back(){

        $DataDir = "databak/";
        $lists = $this->MyScandir('databak/');

        foreach($lists as $k => $v){
            if($k < 2){
                unset($lists[$k]);
            }else{
                $path =  $DataDir .$v;
                $time = date('Y-m-d H:i:s',filemtime($path));
                $size = (filesize($path)/1024);
                $data[] = array('id' => $k-1 , 'title'=>$v,'time'=>date('Y-m-d H:i:s',filemtime($path)),'size'=>sprintf("%.2f",$size));
            }
        }

        $this->assign("datadir",$DataDir);
        $this->assign("list", $data);
        $this->show();
    }


    public function action(){
        
        $action = I('get.act');
        $DataDir = "databak/";
        import('ORG.Util.MySQLReback');
        $config = array(
                'host' => C('DB_HOST'),
                'port' => C('DB_PORT'),
                'userName' => C('DB_USER'),
                'userPassword' => C('DB_PWD'),
                'dbprefix' => C('DB_PREFIX'),
                'charset' => 'UTF8',
                'path' => $DataDir,
                'isCompress' => 0, //是否开启gzip压缩
                'isDownload' => 0
        );

        $mr = new MySQLReback($config);
        $mr->setDBName(C('DB_NAME'));

        if ($action == 'bac') {
            $mr->backup();
            $this->success( '数据库备份成功！',U('Model/back'));
        }elseif ($action == 'rel') {
                $mr->recover($_GET['file']);
                $this->success( '数据库还原成功！',U('Model/back'));
        }elseif ($action == 'del') {
                if (@unlink($DataDir . $_GET['file'])) {
                     $this->success( '数据库删除成功！',U('Model/back'));
                } else {
                    $this->error('删除失败！',U('Model/back'));
                }
        }
        if ($action == 'dow') {
                function DownloadFile($fileName) {
                    ob_end_clean();
                    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                    header('Content-Description: File Transfer');
                    header('Content-Type: application/octet-stream');
                    header('Content-Length: ' . filesize($fileName));
                    header('Content-Disposition: attachment; filename=' . basename($fileName));
                    readfile($fileName);
                }
                DownloadFile($DataDir . $_GET['file']);
             
        }
   }

    private function MyScandir($FilePath = './', $Order = 0) {
        $FilePath = opendir($FilePath);
        while (false !== ($filename = readdir($FilePath))) {
            $FileAndFolderAyy[] = $filename;
        }
        $Order == 0 ? sort($FileAndFolderAyy) : rsort($FileAndFolderAyy);
        return $FileAndFolderAyy;
    }
}